package com.lx.note.web;

import com.lx.common.constant.SessionConstant;
import com.lx.common.vo.Message;
import com.lx.note.entity.UserBO;
import com.lx.note.entity.UserLoginDTO;
import com.lx.note.entity.UserRegisterDTO;
import com.lx.note.service.itf.UserService;
import io.swagger.annotations.ApiModel;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * Created by Administrator on 2018/5/23 0023.
 */
@ApiModel(value = "管理用户信息的类")
@RestController
@RequestMapping(value="/user")
public class UserController extends BaseController{


    @Resource
    private UserService userService;

    @PostMapping(value = "/login.do")
    public ResponseEntity login(@RequestBody UserLoginDTO userLoginDTO,BindingResult bindingResult, HttpSession session){
        if (bindingResult.hasErrors()){
            return ResponseEntity.ok(Message.fail("参数错误"));
        }
        Message<UserBO> message = userService.login(userLoginDTO);
        if(Message.CODE_SUCCESS ==message.getCode()) {
            //登录成功 session 中保存用户信息
            session.setAttribute(SessionConstant.KEY_LOGIN_USER, message.getData());
            return ResponseEntity.ok(Message.ok("登录成功!"));
        }
        return ResponseEntity.ok(Message.fail("登录失败"));

    }

    @PostMapping(value = "/register.do")
    public ResponseEntity regist(@Validated @RequestBody UserRegisterDTO userRegisterDTO){
        Message message = userService.regist(userRegisterDTO);
        return ResponseEntity.ok(message);
    }


}
